package com.service;


import com.dao.CourseDAO;
import com.dao.StudentDAO;
import com.pojo.Course;
import com.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class StudentService {

    @Autowired
    private StudentDAO studentDAO;

    @Autowired
    private CourseDAO  courseDAO;

    /**
     * 新增学生
     * @param student
     * @return
     */
    public Student addStudent(Student student)
    {
      return  studentDAO.save(student);
    }

    /**
     * 学生选课
     * @param sid   学生编号
     * @param cids  课程ID
     */
    public void studentXuanKe(int sid, List<Integer> cids)
    {
        //根据主键查找学生
       Student stu = studentDAO.findById(sid).get();
       //批量查找课程
       List<Course> list = courseDAO.findAllById(cids);
       //选课
       //stu.getCoursesList().clear(); //原来的不要
       //  stu.getCoursesList().addAll(list);
        //stu.setCoursesList(list); //添加到集合
        // studentDAO.save(stu);

        //保存学生
       // studentDAO.save(stu);



    }

    public  Student findById(int sid)
    {
        return studentDAO.findById(sid).get();
    }

    public List<Course> getCoursesByStudent(int sid)
    {
       /* Student stu = studentDAO.findById(sid).get();
        List list = stu.getCoursesList();
        System.out.println(list);
        return list;*/
       return null;
    }





}
